import time

import Stock_Index as s_i
from Stock_List import Stock_List as s_l
import multiprocessing as mp


def start(code):
    socks_list = s_l().stock_list
    # print("-------------------------------------------------------------")
    # print("当前进度：", exec_num, "/", socks_list_num)
    stock_index = s_i.Stock_Index()
    stock_index.stock_info = {code: socks_list[code]}

    # 数字表示该数值在网页表格中的索引
    data_type_info = {
        "code": code,
        "stock_name": socks_list[code],
        "html": {
            "zcfzb": {
                "info": {
                    "advance_charge": 10,  # 预付款项（资产负债表,单位：万元）
                    "receivable": [8, "+", 9],  # 应收票据及应收账款(资产负债表，单位：万元)
                },
                "url": "http://quotes.money.163.com/f10/zcfzb_" + code + ".html?type=year"
            },
            "lrb": {
                "info": {
                    "gross_revenue": 1,  # 营业总收入(利润表，单位：万元)
                    "net_profit": 40,  # 净利润(利润表,单位：万元)
                },
                "url": "http://quotes.money.163.com/f10/lrb_" + code + ".html?type=year",
            },
            "zycwzb": {
                "info": {
                    "free_cash_flow": 12,  # 经营活动产生的现金流量净额(万元)
                },
                "url": "http://quotes.money.163.com/f10/zycwzb_" + code + ",year.html"
            }
        },
        "csv": {
            "zycwzb": {
                "info": {
                    "profit_margin": 1,  # 净利润率
                    "operating_profit_margin": 5,  # 营业利润率
                    "roe": 8,  # 净资产收益率
                    "gm": 12,  # 毛利率
                },
                "url": "http://quotes.money.163.com/service/zycwzb_" + code + ".html?type=year&part=ylnl"
            }
        }
    }

    stock_index.data_type_info = data_type_info
    stock_index.is_del_csv = 0

    # 初始化数据库
    stock_index.mysql_conn = s_i.s_f.connect_mysql("127.0.0.1", 3306, "root", "a5556968", "stock")

    stock_index.run()

    stock_index.mysql_conn.close()
    # exec_num = exec_num + 1
    # print(code, socks_list[code], "结束")


if __name__ == '__main__':
    print("开始", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) )
    stock_list = s_l().stock_list
    pool = mp.Pool()  # 多少个核心就多少爬虫进程并发
    for k in stock_list:
        pool.apply_async(start, (k,))
    pool.close()
    pool.join()
    print("结束", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) )
